var numberFormate = iot.getNumberFormat("0");
//总碳排
var subLevelDevMap = {
    713 :{
        ele : 'E001',
        gas : 'G001'
    },
    714 :{
        ele : 'E009'
    },
    715 :{
        ele : 'E070'
    },
    716 :{
        ele : 'E276'
    },
    717 :{
        ele : 'E037',
        gas : 'G059'
    },
    718 :{
        ele : 'E401',
        gas : 'G002'
    }

}
dev.action = function(resp,param){
    var theTime = param.time;
    var workSpace = param.workSpace;
    var jsData =  iot.list();
    var db = iot.db();
    var timeSub = parseInt(theTime.substring(0,4));

    var coldResult = '';
    var electricityResult = '';
    var gasResult = '';
    var totalCold = 0;
    if(timeSub >= 2019){
        coldResult = db.qry("select esp.name,SUM(dev_win.dvalue) totalSum from iot_dev_data_win dev_win," +
            " (select * from energy_struc_prop where type = 2 and name = '碳排' and energy_struc_id = "+ workSpace +") esp" +
            " where dev_win.dev_id = esp.dev_id" +
            " and dev_win.data_type = esp.name" +
            " and dev_win.the_time like '"+ theTime +"%'");

        coldResult.forAll(function(item){
            totalCold = item.totalSum;
        });
    }else{
        //工厂  E001._用量*0.734 +  G001.用量*2.355
        if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].ele){
            electricityResult = db.queryOne("select SUM(win.dvalue) totalSum " +
                " FROM iot_dev_data_win win " +
                " LEFT JOIN iot_dev dev on dev.id=win.dev_id " +
                " where 1=1 " +
                " and dev.sn='"+subLevelDevMap[workSpace].ele+"'" +
                " and win.data_type = '_用量'" +
                " and win.the_time like '"+ theTime +"%'");
        }

        if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].gas){
            gasResult = db.queryOne("select SUM(win.dvalue) totalSum " +
                " FROM iot_dev_data_win win " +
                " LEFT JOIN iot_dev dev on dev.id=win.dev_id " +
                " where 1=1 " +
                " and dev.sn='"+ subLevelDevMap[workSpace].gas +"'" +
                " and win.data_type = '用量'" +
                " and win.the_time like '"+ theTime +"%'");
        }

        if(electricityResult && electricityResult.totalSum){
            totalCold = electricityResult.totalSum *0.734;
        }
        if(gasResult && gasResult.totalSum){
            totalCold = totalCold + gasResult.totalSum *2.355;
        }
    }
    var yieldResult = db.qry("select SUM(dvalue) totalSum from iot_dev_data_win" +
        " where dev_id = 278 and the_time like '"+ theTime +"%'");


    var totalYield = yieldResult.getValue();
    var totalUseNum = 0;
    var bicycleUseNum = 0;
    var yieldUseNum = 0;
    totalUseNum = numberFormate.format(totalCold);
    if (totalYield['0'].totalSum != null) {
        bicycleUseNum = numberFormate.format(totalCold / totalYield['0'].totalSum);
    }
    if (totalYield['0'].totalSum != null) {
        yieldUseNum = numberFormate.format(totalYield['0'].totalSum);
    }
    jsData.add({unit: "t", lastPeriod: "+0%", type: "总碳排", useNum: totalUseNum, kpi: "-0%"});
    jsData.add({unit: "t/辆", lastPeriod: "-0%", type: "单车碳排", useNum: bicycleUseNum, kpi: "+0%"});
    jsData.add({unit: "辆", type: "总产量", useNum: yieldUseNum});
    jsData.add({unit: "℃", type: "平均温度", useNum: 27.5});

    resp.setJsData(jsData);
}